package com.qkl.xiatang.app.utils;

import com.qkl.xiatang.app.dto.PageOutDTO;
import com.qkl.xiatang.app.dto.log.JobLogOutDTO;
import com.qkl.xiatang.app.dto.log.JobLogTitleDTO;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.hssf.usermodel.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class ExcelUtils {

    public static HSSFWorkbook createHSSFWorkbook(){
        return new HSSFWorkbook();
    }

    public static HSSFSheet createHSSFSheet(HSSFWorkbook workbook,String sheetName){
        return workbook.createSheet(sheetName);
    }

    public static HSSFWorkbook createJobLog(String sheetName, JobLogOutDTO jobLog){
        return  createJobLog(sheetName,jobLog.getJobLogTitle(),jobLog.getList());
    }

    public static HSSFWorkbook createJobLog(String sheetName,List<JobLogTitleDTO> jobLogTitle,List<Map<String,Object>> dataList ){
        HSSFWorkbook workbook = createHSSFWorkbook();
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setWrapText(true);
        HSSFSheet sheet = createHSSFSheet(workbook,sheetName);
        if(ObjectUtils.isNotEmpty(jobLogTitle) && ObjectUtils.isNotEmpty(dataList)){
            HSSFRow row = sheet.createRow(0);
            createJobLogTitle(cellStyle,row,jobLogTitle);

            for(int i = 0 , size = dataList.size(); i< size ; i++ ){
                row = sheet.createRow(i+1);
                createJobLogData(cellStyle,row,jobLogTitle,dataList.get(i));
            }
        }
        return workbook;
    }
    public static void createJobLogData(HSSFCellStyle cellStyle,HSSFRow row ,List<JobLogTitleDTO> jobLogTitle,Map<String,Object> dataMap){
        for(int i = 0,size = jobLogTitle.size();i < size ;i++){
            Object obj = dataMap.get(jobLogTitle.get(i).getKey());
            String value = toValue(obj);
            createCell(cellStyle,row,i,value);
        }
    }
    private static String toValue(Object object){
        if(null == object){
            return "";
        }
        if(object instanceof Collection){
            Collection collection = (Collection)object;
            if(collection.isEmpty()){
                return "";
            }
            return  String.join(Constants.ENTER_LINE, collection);
        }
        return object.toString();
    }


    public static void createJobLogTitle(HSSFCellStyle cellStyle,HSSFRow row ,List<JobLogTitleDTO> jobLogTitle){
        for(int i = 0,size = jobLogTitle.size();i < size ;i++){
            createCell(cellStyle,row,i,jobLogTitle.get(i).getName());
        }

    }

    public static void createCell(HSSFCellStyle cellStyle,HSSFRow row ,int colunmNo,String cellValue){
        HSSFCell cell = row.createCell(colunmNo);
        cell.setCellStyle(cellStyle);
        cell.setCellValue(new HSSFRichTextString(cellValue));
    }

    public static void main(String[] args) {
        Collection<String> collection = new ArrayList();
        collection.add("a");
        collection.add("b");
        System.out.println(toValue(collection));
        System.out.println("===================");
    }



}
